/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.base.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.base.entity.StoreCardTemplateTimes;
import com.chlitina.store.modules.base.service.StoreCardTemplateTimesService;
import com.chlitina.store.modules.employee.entity.Employee;
import com.chlitina.store.modules.sys.entity.Store;
import com.chlitina.store.modules.sys.utils.StoreUtils;

/**
 * 计次护理卡模板Controller
 * @author liujiang
 * @version 2013-11-02
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/base/storeCardTemplateTimes")
public class StoreCardTemplateTimesController extends BaseController {

	@Autowired
	private StoreCardTemplateTimesService storeCardTemplateTimesService;
	
	@ModelAttribute
	public StoreCardTemplateTimes get(@RequestParam(required=false) Long id) {
		if (id != null){
			return storeCardTemplateTimesService.get(id);
		}else{
			return new StoreCardTemplateTimes();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(@RequestParam(required=false)String currentSelectId, StoreCardTemplateTimes storeCardTemplateTimes, HttpServletRequest request, HttpServletResponse response, Model model) {
		String searchStatus = request.getParameter("searchStatus");
        Page<StoreCardTemplateTimes> page = storeCardTemplateTimesService.find(new Page<StoreCardTemplateTimes>(request, response), storeCardTemplateTimes, searchStatus); 
        model.addAttribute("page", page);
    	model.addAttribute("currentSelectId", currentSelectId);
        model.addAttribute("searchStatus", searchStatus);
		return "modules/base/storeCardTemplateTimesList";
	}

	@RequestMapping(value = "form")
	public String form(StoreCardTemplateTimes storeCardTemplateTimes, Model model) {
		model.addAttribute("storeCardTemplateTimes", storeCardTemplateTimes);
		return "modules/base/storeCardTemplateTimesForm";
	}

	@RequestMapping(value = "save")
	public String save(String pageNo, StoreCardTemplateTimes storeCardTemplateTimes, Model model, RedirectAttributes redirectAttributes) {
		Store store = StoreUtils.getStore();
		storeCardTemplateTimes.setStore(store);
		if (!beanValidator(model, storeCardTemplateTimes)){
			return form(storeCardTemplateTimes, model);
		}
		storeCardTemplateTimesService.save(storeCardTemplateTimes);
		addMessage(redirectAttributes, "保存计次护理卡模板'" + storeCardTemplateTimes.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/base/storeCardTemplateTimes/?pageNo=" + pageNo +"&currentSelectId=" + storeCardTemplateTimes.getId();
	}
	
	@RequestMapping(value = "delete")
	public String delete(StoreCardTemplateTimes storeCardTemplateTimes, RedirectAttributes redirectAttributes) {
		int status = storeCardTemplateTimes.getStatus();
		if (status == StoreCardTemplateTimes.FLAG_ENABLE) {
			storeCardTemplateTimes.setStatus(StoreCardTemplateTimes.FLAG_DISABLE);
		} else {
			storeCardTemplateTimes.setStatus(StoreCardTemplateTimes.FLAG_ENABLE);
		}
		storeCardTemplateTimesService.save(storeCardTemplateTimes);
		addMessage(redirectAttributes, "计次护理卡模板状态更改成功！");
		return "redirect:"+Global.ADMIN_PATH+"/base/storeCardTemplateTimes/";
	}

}
